//
//  LoginViewController.m
//  TimeManager
//
//  Created by 刘凯敏 on 04/12/2016.
//  Copyright © 2016 刘凯敏. All rights reserved.
//

#import "LoginViewController.h"
#import "MBProgressHUD+NJ.h"

#define UserNameKey @"name"
#define PwdKey @"pwd"
#define RmbPwdKey @"rmb_pwd"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdField];
    //读取上次的配置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.nameField.text = [defaults valueForKey:UserNameKey];
    //self.pwdField.text = @"";
    self.rembSwitch.on = [defaults valueForKey:RmbPwdKey];
    if (self.rembSwitch.isOn) {
        self.pwdField.text = [defaults valueForKey:PwdKey];
        self.loginBtn.enabled = YES;
    }

}

-(void) textChange{
    //修改按钮状态
    //    if(self.nameField.text.length && self.pwdField.text.length){
    //        self.loginBtn.enabled = YES;
    //    }
    //    else {
    //        self.loginBtn.enabled = NO;
    //    }
    //等价于
    self.loginBtn.enabled = (self.nameField.text.length && self.pwdField.text.length);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

//跳转前准备工作
/*
    一般在这里给下一个控制器传值
    这个sender是当初performSegueWithIdentifier方法传入的sender
 */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //1.取得目标控制器
    //UIViewController *contactVc = segue.destinationViewController;
    //2.设置标题（传值）
    //contactVc.title = [NSString stringWithFormat:@"%@的日计划列表", self.nameField.text];
}

//登录
- (IBAction)loginAction {
    if(![self.nameField.text isEqualToString:@"admin"]){
        [MBProgressHUD showError:@"账号不存在！"];
        return;
    }
    if(![self.pwdField.text isEqualToString:@"123"]){
        [MBProgressHUD showError:@"密码错误！"];
        return;
    }
    //显示蒙版（遮盖）
    [MBProgressHUD showMessage:@"努力加载中"];
    //模拟两秒跳转，以后要发送网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //移除遮盖
        [MBProgressHUD hideHUD];
        [self performSegueWithIdentifier: @"LoginToContact" sender:nil];
    });
    //存储数据
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.nameField.text forKey:UserNameKey];
    [defaults setObject:self.pwdField.text forKey:PwdKey];
    [defaults setBool:self.rembSwitch.isOn forKey:RmbPwdKey];
    //设置同步
    [defaults synchronize];

}

//触碰背景关闭键盘
- (IBAction)backTap:(id)sender {
    //让nameField控件放弃作为第一响应者
    [self.nameField resignFirstResponder];
    //让pwdField放弃作为第一响应者
    [self.pwdField resignFirstResponder];
}

//完成输入后关闭键盘
- (IBAction)exitEnd:(id)sender {
    //sender放弃作为第一响应者
    [sender resignFirstResponder];
}
@end
